package com.hxh.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/9 21:07
 * 可重入锁
 */
public class LockDemo {

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        new Thread(() -> {
            try {
                // 上锁
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "外层");
                try {
                    // 上锁
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + "内层");
                } finally {
                    lock.unlock();
                }
            } finally {
                // 解锁
                lock.unlock();
            }
        }, "t1").start();
    }
}
